<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 头部 -->
      <div class="tag-head">
        <!-- 表单区域 -->
        <el-row>
          <el-col :span="22">
            <el-form ref="form" :model="form" label-width="80px" inline>
              <el-form-item label="标签名称">
                <el-input v-model="form.tagName" width="20px"></el-input>
              </el-form-item>

              <el-form-item label="状态">
                <el-select v-model="form.state" placeholder="请选择">
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="禁用" value="0"></el-option>
                </el-select>
              </el-form-item>

              <!-- 按钮区域 -->
              <el-form-item>
                <el-button size="small" @click="clearBtn">清除</el-button>
                <el-button type="primary" size="small" @click="searchFn"
                  >搜索</el-button
                >
              </el-form-item>
            </el-form>
          </el-col>

          <el-col :span="2">
            <el-button
              type="success"
              size="small "
              icon="el-icon-edit"
              class="addbtn"
              @click="addBtn"
              >新增标签</el-button
            >
          </el-col>
        </el-row>
        <!-- 提示信息 -->
        <el-alert type="info" show-icon :closable="false">
          数据一共 {{ total }} 条
        </el-alert>
      </div>

      <!-- 主体 表格区域 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="120">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope">
            <span>{{ timeFn(scope.row.addDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.state === 1 ? "已启用" : "已禁用" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="isForbiddenBtn(scope.row.id)">{{
              scope.row.state === 1 ? "禁用" : "启用"
            }}</el-button>
            <el-button
              type="text"
              :disabled="scope.row.state === 1"
              @click="putBtn(scope.row.id)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state === 1"
              @click="delBtn(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="space-between">
        <el-col></el-col>
        <el-col>
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50]"
            :page-size="2"
            layout=" prev, pager, next, sizes, jumper"
            :total="total"
          >
          </el-pagination>
        </el-col>
      </el-row>

      <!-- 添加 / 编辑 弹窗 -->
      <el-dialog width="22%" title="新增目录" :visible.sync="dialogVisible">
        <tagsAdd
          @close="closeFn"
          v-if="dialogVisible"
          :isEdit="isEdit"
          :articleId="articleId"
        ></tagsAdd>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import dayjs from 'dayjs' // ES 2015
import { list, detailAPI, changeState, remove } from '@/api/hmmm/tags.js'
import tagsAdd from '../components/tags-add.vue'

export default {
  data () {
    return {
      form: {
        tagName: null, // 输入框 名称
        state: null, // 输入框 状态
        subjectID: null// 学科ID
      },
      tableData: [], // 表格数据
      currentPage: 1,
      total: 0, // 总条数
      dialogVisible: false, // 弹窗 是否展示
      pagination: {
        page: 1, // 当前页
        pagesize: 10 // 每页条数
      },
      isEdit: false, // 是否为 修改 按钮
      detailsList: {}, // 单个标签详情
      articleId: null // 所选标签id
    }
  },
  components: {
    tagsAdd
  },
  created () {
    this.form.subjectID = null
    console.log(this.$route.query.id)
    console.log(this.$route.query.name)
    this.tagId = Number(this.$route.query.id)
    console.log(this.tagId)
    if (this.tagId && this.$route.query.name) {
      // console.log(this.$route.query.id)
      this.form.subjectID = this.$route.query.id
      this.form.tagName = this.$route.query.name
      this.getList()
    } else {
      this.getList()
    }
  },

  methods: {
    // 获取 数据 列表
    async getList () {
      // console.log({ ...this.pagination, ...this.form })
      const res = await list({ ...this.pagination, ...this.form })
      // console.log(res)
      this.tableData = res.data.items
      this.total = res.data.counts
      // const dayjs = require('dayjs')
      // console.log(dayjs(55662315896532).format('YYYY-MM-DD HH:mm:ss'))
    },

    // 搜索 按钮
    searchFn () {
      this.getList()
    },

    // 清除 按钮
    clearBtn () {
      // this.$refs.form.resetFields()
      this.form = {
        tagName: null,
        state: null
      }
      console.log(this.$route.path)
      console.log(this.$route.fullPath)
    },

    // 添加 / 编辑  按钮
    addBtn () {
      this.isEdit = false
      this.dialogVisible = true
    },

    // 添加 / 编辑 弹窗 关闭 时
    closeReuse () {
      this.isEdit = false
    },

    // 禁用 按钮
    async isForbiddenBtn (id) {
      const res = await detailAPI(id)
      // console.log(res)
      if (res.status === 200) {
        if (res.data.state === 0) {
          await changeState({ id, state: 1 })
          this.$message.success('操作成功')
        } else {
          await changeState({ id, state: 0 })
          this.$message.success('操作成功')
        }
        this.getList()
      }
    },

    // 修改 按钮
    async putBtn (id) {
      this.articleId = id
      this.isEdit = true
      this.dialogVisible = true
    },

    // 删除 按钮
    async delBtn (id) {
      const result = await this.$confirm(
        '此操作将永久删除, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(async () => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (result === 'confirm') {
        await remove({ id })
        this.getList()
        this.$message.success('操作成功')
      }
    },

    // 子传父  关闭弹窗
    closeFn () {
      this.dialogVisible = false
      this.getList()
    },

    // 格式化时间
    timeFn (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },

    handleSizeChange (val) {
      this.pagination.pagesize = val
      this.getList()
      // console.log(`每页 ${val} 条`)
    },

    handleCurrentChange (val) {
      this.pagination.page = val
      this.getList()
      // console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style scoped >
.box-card {
  margin: 10px;
}
::v-deep .el-input__inner {
  height: 32px;
}
.tag-head {
  margin-bottom: 10px;
}
</style>
